package com.hzg.ssm.service.impl;

import com.hzg.ssm.domain.Department;
import com.hzg.ssm.mapper.DepartmentMapper;
import com.hzg.ssm.query.PageResult;
import com.hzg.ssm.query.QueryObject;
import com.hzg.ssm.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ Description: TODO
 * @ Author: 卓哥
 * @ Date: 2020/7/5 18:45
 * @ Version: 1.0
 */
@Service
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    
    public void delete(Long id) {
        departmentMapper.deleteByPrimaryKey(id);
    }

    
    public void insert(Department department) {
        departmentMapper.insert(department);

    }

    
    public Department select(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    
    public List<Department> selectAll() {

        return departmentMapper.selectAll();
    }

    
    @Transactional
    public PageResult<Department> query(QueryObject queryObject) {
        int totalCount = departmentMapper.queryCount(queryObject);
        //获取用户传送过来的两个属性
        int currentPage = queryObject.getCurrentPage();
        int pageSize = queryObject.getPageSize();
        //计算真实的页数
        int totalPage=totalCount%pageSize==0 ? totalCount/pageSize:totalCount/pageSize+1;
        //当数据为0的时候
        if (totalCount<=0){
            return PageResult.empty(pageSize);
        }
        //当前页小于1的时候
        if (currentPage<=0){
            queryObject.setCurrentPage(1);
        }
        //当当前页大于真实的总页数的时候
        if (currentPage>=totalPage){
            queryObject.setCurrentPage(totalPage);
            System.out.println("你妹的");
        }
        return new PageResult<Department>(queryObject.getCurrentPage(),queryObject.getPageSize() , totalCount,  departmentMapper.queryList(queryObject));
    }

    
    public void update(Department department) {
        departmentMapper.updateByPrimaryKey(department);
        int i = 19;
        int d = 30;
        System.out.println(i+d);
    }
}
